/**
 * [1071] 字符串的最大公因子
 *
 * @format
 * @lc app=leetcode.cn id=1071 lang=javascript
 */

// @lc code=start
/**
 * @param {string} str1
 * @param {string} str2
 * @return {string}
 */
var gcdOfStrings = function (str1, str2) {
    let [long, short, result] = ["", "", ""]
    str1.length > str2.length ? ((long = str1), (short = str2)) : ((long = str2), (short = str1))
    for (let i = short.length; i > 0; i--) {
        if (short.length % i == 0 && long.length % i == 0) {
            result = short.slice(0, i)
            if (
                RegExp(`(${result}){${short.length / i}}`).test(short) &&
                RegExp(`(${result}){${long.length / i}}`).test(long)
            )
                return result
        }
    }
    return ""
}
// debugger
// console.log(gcdOfStrings("ABCABC", "ABC"))
// @lc code=end
